Add support for multiple verbs.

Darren Cauthon 9 years ago
parent
commit
e0a7c67dcc
2 changed files with 76 additions and 1 deletions
  1. 2 1
      app/models/agents/webhook_agent.rb
  2. 74 0
      spec/models/agents/webhook_agent_spec.rb

+ 2 - 1
app/models/agents/webhook_agent.rb

@@ -39,7 +39,8 @@ module Agents
39 39
 
40 40
     def receive_web_request(params, method, format)
41 41
       secret = params.delete('secret')
42
-      return ["Please use POST requests only", 401] unless method == "post" || options['verbs']
42
+      verbs = (options['verbs'] ? options['verbs'] : 'post').split(';')
43
+      return ["Please use #{verbs.first.upcase} requests only", 401] unless verbs.include?(method)
43 44
       return ["Not Authorized", 401] unless secret == interpolated['secret']
44 45
 
45 46
       [payload_for(params)].flatten.each do |payload|

+ 74 - 0
spec/models/agents/webhook_agent_spec.rb

@@ -82,6 +82,80 @@ describe Agents::WebhookAgent do
82 82
 
83 83
       end
84 84
 
85
+      context "accepting only get" do
86
+
87
+        before { agent.options['verbs'] = 'get' }
88
+
89
+        it "should accept GET" do
90
+          out = nil
91
+          expect {
92
+            out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "get", "text/html")
93
+          }.to change { Event.count }.by(1)
94
+          expect(out).to eq(['Event Created', 201])
95
+        end
96
+
97
+        it "should not accept POST" do
98
+          out = nil
99
+          expect {
100
+            out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "post", "text/html")
101
+          }.to change { Event.count }.by(0)
102
+          expect(out).to eq(['Please use GET requests only', 401])
103
+        end
104
+
105
+      end
106
+
107
+      context "accepting only post" do
108
+
109
+        before { agent.options['verbs'] = 'post' }
110
+
111
+        it "should not accept GET" do
112
+          out = nil
113
+          expect {
114
+            out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "get", "text/html")
115
+          }.to change { Event.count }.by(0)
116
+          expect(out).to eq(['Please use POST requests only', 401])
117
+        end
118
+
119
+        it "should accept POST" do
120
+          out = nil
121
+          expect {
122
+            out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "post", "text/html")
123
+          }.to change { Event.count }.by(1)
124
+          expect(out).to eq(['Event Created', 201])
125
+        end
126
+
127
+      end
128
+
129
+      context "accepting only put" do
130
+
131
+        before { agent.options['verbs'] = 'put' }
132
+
133
+        it "should accept PUT" do
134
+          out = nil
135
+          expect {
136
+            out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "put", "text/html")
137
+          }.to change { Event.count }.by(1)
138
+          expect(out).to eq(['Event Created', 201])
139
+        end
140
+
141
+        it "should not accept GET" do
142
+          out = nil
143
+          expect {
144
+            out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "get", "text/html")
145
+          }.to change { Event.count }.by(0)
146
+          expect(out).to eq(['Please use PUT requests only', 401])
147
+        end
148
+
149
+        it "should not accept POST" do
150
+          out = nil
151
+          expect {
152
+            out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "post", "text/html")
153
+          }.to change { Event.count }.by(0)
154
+          expect(out).to eq(['Please use PUT requests only', 401])
155
+        end
156
+
157
+      end
158
+
85 159
     end
86 160
 
87 161
   end